package com.lanou.renrenxin.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easemob.chat.EMContactListener;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.custommodule.RotateLoading;
import com.lanou.renrenxin.tools.LogUtils;
import com.parse.ParseAnalytics;
import com.parse.ParseQuery;

import java.util.List;


/**
 * Created by zy on 16/3/4.
 * 搜索好友,显示好友请求activity
 */
public class AddContactActivity extends BaseActivity implements View.OnClickListener {
    private EditText editTextsearch;
    private ImageButton btnSearch;
    private Button btnAddContact, btnBack;
    private TextView tvTitle,tvName;
    private String addUserName;
    private LinearLayout showSearchview;
    private RotateLoading rotateLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ParseAnalytics.trackAppOpenedInBackground(getIntent());
        setContentView(R.layout.activity_addcontact);
        editTextsearch = (EditText) findViewById(R.id.serachcontact_et);
        btnSearch = (ImageButton) findViewById(R.id.searchcontact_btn);
        showSearchview = (LinearLayout) findViewById(R.id.ll_user);
        btnAddContact = (Button) findViewById(R.id.addcontact_btn);
        btnAddContact.setOnClickListener(this);
        btnSearch.setOnClickListener(this);
        btnBack = (Button) findViewById(R.id.btnBack);
        tvTitle = (TextView) findViewById(R.id.txtTitle);
        tvName = (TextView) findViewById(R.id.name);
        tvTitle.setText("添加好友");
        btnBack.setVisibility(View.VISIBLE);
        btnBack.setOnClickListener(this);
        rotateLoading = (RotateLoading) findViewById(R.id.rotateloading);
        listenRequest();
    }

    //监听好友状态事件
    private void listenRequest() {
        EMContactManager.getInstance().setContactListener(new EMContactListener() {

            @Override
            public void onContactAgreed(String username) {
                //好友请求被同意
            }

            @Override
            public void onContactRefused(String username) {
                //好友请求被拒绝
            }

            @Override
            public void onContactInvited(String username, String reason) {
                //收到好友邀请
                BaseActivity.ShowToast("收到好友邀请");
            }

            @Override
            public void onContactDeleted(List<String> usernameList) {
                //被删除时回调此方法
            }


            @Override
            public void onContactAdded(List<String> usernameList) {
                //增加了联系人时回调此方法
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.searchcontact_btn:
                searchContact();
                break;
            case R.id.addcontact_btn:
                addContact();
                break;
            case R.id.btnBack:
                finish();
                break;
        }
    }


    //在服务器中查找联系人
    private void searchContact() {

        final String name = editTextsearch.getText().toString();

        if (TextUtils.isEmpty(name)) {
            BaseActivity.ShowToast("请输入用户id");
            showSearchview.setVisibility(View.GONE);

        } else {
//            rotateLoading.start();
//            ParseQuery<ParseObject> pq = ParseQuery.getQuery("UserInfo");
//            Log.d("333", name);
//            pq.whereEqualTo("user_name", name);
//            pq.findInBackground(new FindCallback<ParseObject>() {
//                @Override
//                public void done(List<ParseObject> objects, ParseException e) {
//
//                    if (objects != null) {
//                        Log.d("object", "objects:" + objects);
//                        //在环信服务器中有该用户
//                        tvName.setText(name);
//                        showSearchview.setVisibility(View.VISIBLE);
//                        LogUtils.i("111", "have");
//                        rotateLoading.stop();
//                    } else {
//                        Toast.makeText(AddContactActivity.this, "该用户不存在", Toast.LENGTH_SHORT).show();
//                        LogUtils.i("222", "none");
//                        showSearchview.setVisibility(View.GONE);
//                        rotateLoading.stop();
//                    }
//                }
//            });
            String myId = "645";
            ParseQuery query = new ParseQuery("UserInfo");
        }
    }

    //添加联系人
    private void addContact() {
//        //参数为要添加的好友的username和添加理由
//        EMContactManager.getInstance().addContact(toAddUsername, reason);//需异步处理
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String reason = "我是bug";
                    String userid = "95279527";
                    EMContactManager.getInstance().addContact(userid, reason);
                    LogUtils.i("userid", userid);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            BaseActivity.ShowToast("添加好友请求发送");
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }


}
